﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication4.Models;

namespace MvcApplication4.Controllers
{
    [Authorize]
    public class CategoryController : Controller
    {
        
        private ForumsEntities2 db = new ForumsEntities2();

        //
        // GET: /Category/

        public ActionResult Index()
        {
            var categories = db.Categories.Include("User");
            return View(categories.ToList());
        }

        //
        // GET: /Category/Details/5

        public ActionResult Details(long id = 0)
        {
            Category category = db.Categories.Single(c => c.CategoryId == id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // GET: /Category/Create

        public ActionResult Create()
        {
            ViewBag.Creator = new SelectList(db.Users, "UserId", "UserName");
            return View();
        }

        //
        // POST: /Category/Create

        [HttpPost]
        public ActionResult Create(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.AddObject(category);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Creator = new SelectList(db.Users, "UserId", "UserName", category.Creator);
            return View(category);
        }

        //
        // GET: /Category/Edit/5

        public ActionResult Edit(long id = 0)
        {
            Category category = db.Categories.Single(c => c.CategoryId == id);
            if (category == null)
            {
                return HttpNotFound();
            }
            ViewBag.Creator = new SelectList(db.Users, "UserId", "UserName", category.Creator);
            return View(category);
        }

        //
        // POST: /Category/Edit/5

        [HttpPost]
        public ActionResult Edit(Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Attach(category);
                db.ObjectStateManager.ChangeObjectState(category, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.Creator = new SelectList(db.Users, "UserId", "UserName", category.Creator);
            return View(category);
        }

        //
        // GET: /Category/Delete/5

        public ActionResult Delete(long id = 0)
        {
            Category category = db.Categories.Single(c => c.CategoryId == id);
            if (category == null)
            {
                return HttpNotFound();
            }
            return View(category);
        }

        //
        // POST: /Category/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            Category category = db.Categories.Single(c => c.CategoryId == id);
            db.Categories.DeleteObject(category);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        //
        //
        public ActionResult ContainedThreads(long id)
        {
            var categories = db.Threads.Where("it.Category = " + id);
            return View("_Category_Thread_Partial", categories.ToList());
        }


        public ActionResult Category_Thread_Post(long id)
        {
            return Redirect("../../Thread/Details/"+id);
        }

    }
}